package Tree;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Tree
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-26  09:02
 * @Description: TODO
 * @Version: 1.0
 */
public class 二叉树路径总和437 {

    int count = 0;

    public int pathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return 0;
        }
        rootSum(root, targetSum);
        pathSum(root.left, targetSum);
        pathSum(root.right, targetSum);
        return count;
    }

    private void rootSum(TreeNode root, int targetSum) {
        if (root == null) {
            return;
        }
        targetSum = targetSum - root.val;
        if (targetSum == 0) {
            count++;
        }
        rootSum(root.left, targetSum);
        rootSum(root.right, targetSum);
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
